<template>
    <div>
        <el-card>
            <el-form :inline="true" :model="queryForm" ref="queryFormRef">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="queryForm.name" clearable />
                </el-form-item>
                <el-form-item label="是否展示" prop="status">
                    <el-select v-model="queryForm.status" clearable>
                        <el-option label="展示" :value="1" />
                        <el-option label="不展示" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :disabled="loading" @click="submitForm"
                        v-has="'btn.queryCategory'">查询</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card>
            <template #header>
                <el-button icon="Plus" @click="handleAdd" v-has="'btn.addCategory'">添加分类</el-button>
            </template>
            <el-table :data="records" style="width: 100%;" v-loading="loading" border>
                <el-table-column type="index" label="序号" width="80" align="center" />
                <el-table-column prop="name" label="分类名称" min-width="100" align="center" />
                <el-table-column prop="sort" label="排序" min-width="100" align="center" />
                <el-table-column label="是否展示" min-width="100" align="center">
                    <template #default="{ row }">
                        <el-switch v-model="row.status" :active-value="1" :inactive-value="0" :loading="switchLoading"
                            @change="handleStatus(row)" />
                    </template>
                </el-table-column>
                <el-table-column label="操作" min-width="320" align="center">
                    <template #default="{ row }">
                        <el-button icon="Edit" @click="handleEdit(row)" v-has="'btn.editCategory'">编辑</el-button>
                        <el-button type="danger" icon="Delete" @click="handleDelete(row)"
                            v-has="'btn.deleteCategory'">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-dialog :title="isEdit ? '编辑分类' : '添加分类'" v-model="dialogVisible" :close-on-click-modal="false">
            <AddOrEditCategory v-if="dialogVisible" :is-edit="isEdit" :id="id" @success="onSuccess"></AddOrEditCategory>
        </el-dialog>
    </div>
</template>

<script setup lang="ts" name="Category">
import { onMounted, ref } from 'vue';
import { ElMessage, ElMessageBox, type FormInstance } from 'element-plus';
import AddOrEditCategory from './components/AddOrEditCategory.vue';
import type { CategoryType } from '@/api/wx/category/types';
import { list, categoryRemove, categoryChangeStatus } from '@/api/wx/category';
import { reactive } from 'vue';

// ---------- 查询 ----------
const loading = ref(false)
const queryForm = reactive({ name: '', status: 1 })
const queryFormRef = ref<FormInstance>()
let records = ref<CategoryType[]>([])

onMounted(() => submitForm())

const submitForm = async () => {
    loading.value = true
    const data = await list(queryForm);
    if (data.code === 200) {
        records.value = data.data
    }
    loading.value = false
}

// ---------- 添加/编辑/删除 ----------
const isEdit = ref(false)
const dialogVisible = ref(false)
const id = ref()

const handleAdd = () => {
    dialogVisible.value = true
    isEdit.value = false
}
const handleEdit = (row: CategoryType) => {
    dialogVisible.value = true
    isEdit.value = true
    id.value = row.id
}
const handleDelete = async (row: CategoryType) => {
    ElMessageBox.confirm(`确认删除分类【${row.name}】吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const data = await categoryRemove(row.id as number)
        if (data.code === 200) {
            ElMessage.success('删除成功');
            submitForm();
        } else {
            ElMessage.error('删除失败');
        }
    });
}
const onSuccess = () => {
    dialogVisible.value = false
    submitForm()
}

// ---------- 是否展示 ----------
const switchLoading = ref(false)

const handleStatus = async (row: CategoryType) => {
    try {
        switchLoading.value = true
        const data = await categoryChangeStatus(row.id as number, row.status as number)
        if (data.code === 200) {
            ElMessage.success('修改成功');
            submitForm();
        } else {
            ElMessage.error('修改失败');
        }
    } finally {
        switchLoading.value = false
    }
}
</script>

<style scoped lang="scss">
:deep(.el-card) {
    margin-bottom: 20px;
    min-width: 600px;

    .el-form {

        .el-input {
            width: 240px;
        }

        .el-select {
            width: 240px;

        }

        .el-form-item {
            margin-bottom: 0;
        }
    }
}
</style>